/**
 * 
 */
package com.aibili.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.aibili.pojo.FileInfo;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.ImageUtil;

/**
 * @author qiuch
 * 
 */
@Service("fileService")
@Transactional
public class FileService extends BaseService {

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public long saveMomentImage(MultipartFile file) {
		long fileid = 0;
		try {
			if (file != null) {
				// 取得当前上传文件的文件名称
				String myFileName = file.getOriginalFilename();
				// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
				if (!CommonUtils.isEmptyString(myFileName)) {
					Date now = new Date();
					String savePath = CommonUtils.getTimeFormat(now, "yyyyMMdd") + File.separator;
					String originPath = CommonUtils.originImagePath() + savePath;
					String cachePath = CommonUtils.cacheImagePath() + savePath;
					// 定义上传路径
					CommonUtils.checkPath(originPath);
					CommonUtils.checkPath(cachePath);

					String extension = myFileName.substring(myFileName.lastIndexOf(".")).toLowerCase();
					String filename = CommonUtils.getTimeFormat(now, "yyyyMMddhhmmssSSS") + "_"
							+ (int) (Math.random() * 100) + extension;
					File localOriginFile = new File(originPath + filename);
					file.transferTo(localOriginFile);

					BufferedImage bufferedImage = ImageIO.read(localOriginFile);
					int width = bufferedImage.getWidth();
					int height = bufferedImage.getHeight();
					ImageUtil imageUtil = new ImageUtil();
					if (height >= width) {
						imageUtil.setCompressHeight(500);
					} else {
						imageUtil.setCompressWidth(500);
					}
					imageUtil.imageThumb(originPath + filename, cachePath + filename);
					// 文件表留档
					FileInfo fileInfo = new FileInfo();
					fileInfo.setFile_name((savePath + filename).replace(File.separator, "/"));
					fileInfo.setCreate_time(now);
					dataDao.addObject(fileInfo);
					fileid = fileInfo.getId();
				}
			}
		} catch (Exception e) {
			return 0l;
		}
		return fileid;
	}

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public String saveImage(MultipartFile file) {
		String filePath = "";
		try {
			if (file != null) {
				// 取得当前上传文件的文件名称
				String myFileName = file.getOriginalFilename();
				// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
				if (!CommonUtils.isEmptyString(myFileName)) {
					Date now = new Date();
					String savePath = CommonUtils.getTimeFormat(now, "yyyyMMdd") + File.separator;
					String originPath = CommonUtils.originImagePath() + savePath;
					String cachePath = CommonUtils.cacheImagePath() + savePath;
					// 定义上传路径
					CommonUtils.checkPath(originPath);
					CommonUtils.checkPath(cachePath);

					String extension = myFileName.substring(myFileName.lastIndexOf(".")).toLowerCase();
					String filename = CommonUtils.getTimeFormat(now, "yyyyMMddhhmmssSSS") + "_"
							+ (int) (Math.random() * 100) + extension;
					File localOriginFile = new File(originPath + filename);
					file.transferTo(localOriginFile);

					BufferedImage bufferedImage = ImageIO.read(localOriginFile);
					int width = bufferedImage.getWidth();
					int height = bufferedImage.getHeight();
					ImageUtil imageUtil = new ImageUtil();
					imageUtil.setCompressHeight(height);
					imageUtil.setCompressWidth(width);
					imageUtil.setCompressQuaity(0.65f);
					imageUtil.compressImage(originPath + filename, true, cachePath + filename);
					filePath = (savePath + filename).replace(File.separator, "/");
					// 文件表留档
					FileInfo fileInfo = new FileInfo();
					fileInfo.setFile_name(filePath);
					fileInfo.setCreate_time(now);
					dataDao.addObject(fileInfo);
				}
			}
		} catch (Exception e) {
			return "";
		}
		return filePath;
	}

	/**
	 * 没有缩略图
	 * 
	 * @param file
	 * @return
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public String saveFile(MultipartFile file) {
		String filePath = "";
		try {
			if (file != null) {
				// 取得当前上传文件的文件名称
				String myFileName = file.getOriginalFilename();
				// 如果名称不为“”,说明该文件存在，否则说明该文件不存在
				if (!CommonUtils.isEmptyString(myFileName)) {
					Date now = new Date();
					String savePath = CommonUtils.getTimeFormat(now, "yyyyMMdd") + File.separator;
					String normalFilePath = CommonUtils.normalFilePath() + savePath;
					// 定义上传路径
					CommonUtils.checkPath(normalFilePath);

					String extension = myFileName.substring(myFileName.lastIndexOf(".")).toLowerCase();
					String filename = CommonUtils.getTimeFormat(now, "yyyyMMddhhmmssSSS") + "_"
							+ (int) (Math.random() * 100) + extension;
					File localOriginFile = new File(normalFilePath + filename);
					file.transferTo(localOriginFile);
					filePath = (savePath + filename).replace(File.separator, "/");
					// 文件表留档
					FileInfo fileInfo = new FileInfo();
					fileInfo.setFile_name(filePath);
					fileInfo.setCreate_time(now);
					dataDao.addObject(fileInfo);
				}
			}
		} catch (Exception e) {
			return "";
		}
		return filePath;
	}

	public FileInfo getById(long id) {
		FileInfo info = null;
		info = dataDao.getObjectById(FileInfo.class, id);
		return info;
	}

}
